﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.GoodsDelivery;
using MINERP.COM;

namespace ERPGoodsDelivery.Return
{
    public partial class FrmDispose : Form
    {
        //属性
        private static FrmDispose _instance;
        public static FrmDispose GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmDispose();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        //参数
        private GoodsReturnBLL bll = new GoodsReturnBLL();
        private GoodsDeliveryBLL userBll = new GoodsDeliveryBLL();

        public FrmDispose()
        {
            InitializeComponent();
        }

        private void FrmDispose_Load(object sender, EventArgs e)
        {
            BinUser();
            BindReturnOrderInfoNotDispose();
            SetDefaultTime();
        }

        //刷新未处理的退件产品
        private void tsRefreshFetched_Click(object sender, EventArgs e)
        {
            try
            {
                BindReturnOrderInfoNotDispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //单击(未处理)
        private void gvReturnOrderInfo_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                string disposeUserName = gvReturnOrderInfo.GetRowCellValue(e.RowHandle, "DisposeUserName").ToString();
                string disposeRecord = gvReturnOrderInfo.GetRowCellValue(e.RowHandle, "DisposeRecord").ToString();
                SetDisposeInfo(disposeUserName, disposeRecord);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //焦点发生改变(未处理)
        private void gvReturnOrderInfo_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                try
                {
                    string disposeUserName = gvReturnOrderInfo.GetRowCellValue(e.FocusedRowHandle, "DisposeUserName").ToString();
                    string disposeRecord = gvReturnOrderInfo.GetRowCellValue(e.FocusedRowHandle, "DisposeRecord").ToString();
                    SetDisposeInfo(disposeUserName, disposeRecord);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //处理退件产品
        private void tsSaveReturnInfo_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvReturnOrderInfo.SelectedRowsCount == 0)
                {
                    MessageBox.Show("无退件产品被选中！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (DialogResult.Yes == MessageBox.Show("确定处理选中的退件产品吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                {
                    DisposeReturnOrderInfo();
                    BindReturnOrderInfoNotDispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //退件产品状态从已处理改回未处理(即已取件)
        private void tsModifyStatus_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvReturnOrderInfoDisposed.SelectedRowsCount == 0)
                {
                    MessageBox.Show("无退件产品存在！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (DialogResult.Yes == MessageBox.Show("确定要将选中的退件产品状态改回未处理吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                {
                    int returnInfoId = Convert.ToInt32(gvReturnOrderInfoDisposed.GetRowCellValue(gvReturnOrderInfoDisposed.GetSelectedRows()[0], "ReturnInfoId"));
                    ModifyReturnInfoStatusToFetched(returnInfoId);
                    BindReturnOrderInfoDisposed();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //tab
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (tabControl1.SelectedTab.Name == "tpNotDispose")
                {
                    BindReturnOrderInfoNotDispose();
                }
                else if (tabControl1.SelectedTab.Name == "tpDisposed")
                {
                    BindReturnOrderInfoDisposed();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //按时间范围显示已处理的退件产品
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                BindReturnOrderInfoDisposed();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //-----------------公共方法-----------------
        private void BinUser()
        {
            try
            {
                DataTable dt = userBll.GetDeliveryWorkers();
                cbUser.DisplayMember = "UserName";
                cbUser.ValueMember = "UserId";
                cbUser.DataSource = dt;
                cbUser.Text = "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 绑定未处理的退件产品
        /// <summary>
        /// 绑定未处理的退件产品
        /// </summary>
        private void BindReturnOrderInfoNotDispose()
        {
            try
            {
                int status = (int)ReturnOrderInfoStatus.Fetched;
                DataTable dt = bll.GetReturnOrderInfosByStatus(status,DateTime.Now,DateTime.Now);
                gcReturnOrderInfo.DataSource = dt;
                gcReturnOrderInfo.RefreshDataSource();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 绑定已处理的退件产品
        /// <summary>
        /// 绑定已处理的退件产品
        /// </summary>
        private void BindReturnOrderInfoDisposed()
        {
            try
            {
                int status = (int)ReturnOrderInfoStatus.Disposed;
                DateTime start = dtStart.Value;
                DateTime end = dtEnd.Value;
                DataTable dt = bll.GetReturnOrderInfosByStatus(status,start,end);
                gcReturnOrderInfoDisposed.DataSource = dt;
                gcReturnOrderInfoDisposed.RefreshDataSource();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 设置处理信息
        /// <summary>
        /// 设置处理信息
        /// </summary>
        /// <param name="disposeUserName"></param>
        /// <param name="disposeRecord"></param>
        private void SetDisposeInfo(string disposeUserName, string disposeRecord)
        {
            try
            {
                if (disposeUserName.Length > 0)
                {
                    cbUser.Text = disposeUserName;
                }
                txtDisposeRecord.Text = disposeRecord;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 处理退件产品
        /// <summary>
        /// 处理退件产品
        /// </summary>
        private void DisposeReturnOrderInfo()
        {
            try
            {
                int userId = Convert.ToInt32(cbUser.SelectedValue);
                string disposeRecord = txtDisposeRecord.Text;
                int status = (int)ReturnOrderInfoStatus.Disposed;
                int returnInfoId = Convert.ToInt32(gvReturnOrderInfo.GetRowCellValue(gvReturnOrderInfo.GetSelectedRows()[0], "ReturnInfoId"));
                bll.DisposeReturnOrderInfo(returnInfoId, userId, disposeRecord, status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 将退件产品的状态从已处理改回未处理(即已取件)
        /// <summary>
        /// 将退件产品的状态从已处理改回未处理(即已取件)
        /// </summary>
        /// <param name="returnInfoId"></param>
        private void ModifyReturnInfoStatusToFetched(int returnInfoId)
        {
            try
            {
                int status = (int)ReturnOrderInfoStatus.Fetched;
                bll.ModifyReturnOrderInfoStatus(returnInfoId, status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        private void SetDefaultTime()
        {
            dtStart.Value = DateTime.Now.AddDays(-10);
            dtEnd.Value = DateTime.Now.AddDays(1);
        }
    }
}
